package me.firstapp.service.system.impl;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import me.firstapp.common.exception.ServiceException;
import me.firstapp.common.exception.StatusHouse;
import me.firstapp.common.utils.Page;
import me.firstapp.common.utils.StrUtils;
import me.firstapp.module.system.EmailConfig;
import me.firstapp.repository.system.EmailConfigRepository;
import me.firstapp.service.system.EmailConfigService;

@Service
public class EmailConfigServiceImpl implements EmailConfigService {

	@Autowired
	private EmailConfigRepository emailConfigRepository;

	@Override
	public EmailConfig findByApiClientIdAndStatus(Long apiClientId, Integer status) {
		if (apiClientId == null) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "apiClientId");
		}
		if (status == null) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "状态");
		}
		return emailConfigRepository.findByApiClientIdAndStatus(apiClientId, status);
	}

	@Override
	public EmailConfig addEmailConfig(Long apiClientId, String apiUrl, String apiUser, String apiKey, String mailFrom) {
		if (apiClientId == null) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "apiClientId");
		}
		if (StrUtils.isNULL(apiUrl)) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "apiUrl");
		}
		if (StrUtils.isNULL(apiUser)) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "apiUser");
		}
		if (StrUtils.isNULL(apiKey)) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "apiKey");
		}
		if (StrUtils.isNULL(mailFrom)) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "mailFrom");
		}
		EmailConfig emailConfig = new EmailConfig();
		emailConfig.setApiUrl(apiUrl);
		emailConfig.setApiClientId(apiClientId);
		emailConfig.setApiUser(apiUser);
		emailConfig.setApiKey(apiKey);
		emailConfig.setMailFrom(mailFrom);
		emailConfig.setStatus(0);
		emailConfigRepository.save(emailConfig);
		return emailConfig;
	}

	@Override
	public EmailConfig updateEmailConfig(Long id, Long apiClientId, String apiUrl, String apiUser, String apiKey,
			String mailFrom, Integer status) {
		if (id == null) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "id");
		}
		if (apiClientId == null) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "apiClientId");
		}
		EmailConfig emailConfig = emailConfigRepository.findById(id, apiClientId);
		if (emailConfig == null) {
			throw new ServiceException(StatusHouse.EMAIL_CONFIG_NOT_EXIST);
		}
		if (StrUtils.isNotNULL(apiUrl)) {
			emailConfig.setApiUrl(apiUrl);
		}
		if (StrUtils.isNotNULL(apiUser)) {
			emailConfig.setApiUser(apiUser);
		}
		if (StrUtils.isNotNULL(apiKey)) {
			emailConfig.setApiKey(apiKey);
		}
		if (StrUtils.isNotNULL(mailFrom)) {
			emailConfig.setMailFrom(mailFrom);
		}
		if (status != null) {
			if (status == 1) {
				EmailConfig _emailConfig = emailConfigRepository.findByApiClientIdAndStatus(apiClientId, 1);
				if (_emailConfig != null) {
					throw new ServiceException(StatusHouse.EMAIL_CONFIG_ENABLED_IS_EXIST);
				}
			}
			emailConfig.setStatus(status);
		}
		emailConfigRepository.update(emailConfig);
		return emailConfig;
	}

	@Override
	public void deleteSections(String ids, Long apiClientId) {
		if (StrUtils.isNULL(ids)) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "id");
		}
		if (!ids.contains(",")) {
			EmailConfig emailConfig = emailConfigRepository.findById(Long.valueOf(ids), apiClientId);
			if (emailConfig != null) {
				emailConfigRepository.deleteById(Long.valueOf(ids));
			}
		} else {
			String[] _ids = ids.split(",");
			Long[] _ids_ = new Long[_ids.length];
			for (int i = 0; i < _ids.length; i++) {
				EmailConfig emailConfig = emailConfigRepository.findById(Long.valueOf(_ids[i]), apiClientId);
				if (emailConfig != null) {
					_ids_[i] = Long.valueOf(_ids[i]);
				}
			}
			if (_ids.length > 0) {
				emailConfigRepository.delete(_ids_);
			}
		}
	}

	@Override
	public Page<EmailConfig> findEmailConfigPage(Integer pageNo, Integer pageSize, Map<String, Object> searchParams,
			String sortParam, String sortType, Long apiClientId) {
		return emailConfigRepository.findEmailConfigPage(pageNo, pageSize, searchParams, sortParam, sortType,
				apiClientId);
	}

	@Override
	public EmailConfig findById(Long id, Long apiClientId) {
		if (id == null) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "id");
		}
		if (apiClientId == null) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "apiClientId");
		}
		return emailConfigRepository.findById(id, apiClientId);
	}

}
